--------------------------------------------------------------------------------
-- hlp_typea.lua
--   smartcard development help toolset for ISO14443, typea card
--
-- y.volta ET gmail.com
-- 2011/05/29 17:58:52 
--------------------------------------------------------------------------------

help.TYPEA = {}

local s_iso14443_typea = [[
+-----------------+--------------------------+--------------+
|     Prologue    |       Information        |    Epilogue  |
+-----------------+--------------------------+--------------+
| PCB | CID | NAD |             INF          |      EDC     |
+-----------------+--------------------------+--------------+
]]

local s_iso14443_typea_rats = [[
+------------------------------------------------+
| TL | T0 | TA1 | TB1 | TC1 | T1, ... TK |  CRC  |
+------------------------------------------------+
]]

setmetatable(help.TYPEA, 
{__tostring = function () return s_iso14443_typea; end }
)


function help.TYPEA.dePCB (pcb)

end

function sHex (s)
  return string.format ("%02X", s);
end

-- typea.deATS("0C78F7C40346532015100152")
-- 0C 78  F7 C4 03  46532015100152
function help.TYPEA.deATS (typeaBlock)
  -- FSC table
  -- Inx   0   1   2   3   4   5   6   7   8   (9-F)
  -- fSD  16  24  32  40  48  64  96 128 256   RFU
  local fsc = {16, 24, 32, 40, 48, 64, 96, 128, 256, 0, 0, 0, 0, 0, 0, 0};
  
  print (s_iso14443_typea_rats);
    
  bBlk = bytes.new(8, typeaBlock)
  print ("TL: length " .. sHex(bBlk[0]));
  
  print ("Format Byte: " .. sHex(bBlk[1]));
  print ("  Y1(TC,TB,TA) = (" .. ")");
  print ("  FSCI=" .. sHex(bit.AND(bBlk[1], 0x0F)) .. ", FSC=" .. fsc[bit.AND(bBlk[1], 0x0F)+1]);
end

return help.TYPEA;
